package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.ChannelListParam
import com.cheyun.netsalev3.bean.dm.DmLogin
import com.cheyun.netsalev3.repository.DmSwitchingDealersRepository
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.DmApiClient
import com.cheyun.netsalev3.utils.api.DmBaseResp
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import com.cheyun.netsalev3.view.dm.DmMainActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author Michael
 * @date  2018/12/19 - 17:54.
 */
class DmSwitchingDealersActivityViewModel(var switchingDealersRepository: DmSwitchingDealersRepository) : BaseViewModel() {
    var data = MutableLiveData<List<ChannelListParam>>()
    var listdata = ArrayList<ChannelListParam>()

    init {
        switchingDealersRepository.getData(this::setData)
    }

    var networkState = Transformations.switchMap(data) {
        switchingDealersRepository.networkState
    }

    private fun setData(item: List<ChannelListParam>) {
        var channel = MySharedPreferences.getChannel()
//        if(channel == 0)
//           channel = MySharedPreferences.getLoginData()?.member?.channel!!
        for (i in item) {
            i.IsSelect = i.channel == channel
        }
        listdata.addAll(item)
        data.postValue(listdata)
    }

    fun onClickItem(view: View, item: ChannelListParam) {
        for (i in listdata) {
            i.IsSelect = i.channel == item.channel
        }

        var moth: () -> Unit = {
            MySharedPreferences.putChannel(item.channel)
            var mobile = MySharedPreferences.getMobile()
            var password = MySharedPreferences.getPassword()

            DmApiClient.getInstance().login(mobile, CommonFunc.textToMD5L32(password), item.channel)
                    .subscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : DmApiCallback<DmBaseResp<DmLogin, Any>>() {
                        override fun onSuccess(t: DmBaseResp<DmLogin, Any>) {
                            t.list.apply {
                                MySharedPreferences.putDMCauthkey(this?.auth.toString())
                                MySharedPreferences.putDMMgr(this?.clue_mgr!!)
                                MySharedPreferences.putDmLoginData(this);

//                            view.context.startActivity(Intent(view.context, DmIndexActivity::class.java))
                                view.context.startActivity(Intent(view.context, DmMainActivity::class.java))
                            }
                        }
                        override fun onError(ex: DmApiException) {
                            Log.e("dm login",ex.getMessage())
                            MyToast(ex.getMessage()!!);
                        }
                    })
        }
        switchingDealersRepository.channelhang(item.channel, moth)
    }

    fun refresh() {
        switchingDealersRepository.getData(this::setData)
    }

}